package com.mlnx.ui.service.impl;

import com.mlnx.service.base.service.BaseService;
import com.mlnx.service.common.enums.ResponseEnum;
import com.mlnx.service.common.exception.MutilDeviceServiceException;
import com.mlnx.ui.dao.HealthReportMapper;
import com.mlnx.ui.entity.HealthReport;
import com.mlnx.ui.entity.HealthSport;
import com.mlnx.ui.pojo.form.HealthReportForm;
import com.mlnx.ui.service.HealthReportService;
import com.mlnx.ui.service.HealthSportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class HealthReportServiceImpl extends BaseService<HealthReportMapper, HealthReport> implements HealthReportService {

    @Autowired
    @Override
    public void setBaseMapper(HealthReportMapper baseMapper) {
        super.setBaseMapper(baseMapper);
    }

    @Autowired
    private HealthSportService healthSportService;

    @Override
    @Transactional
    public void saveHealthReport(HealthReportForm healthReportForm) {
        healthReportForm.getHealthReport().setSubmitTime(new Date());
        baseMapper.insert(healthReportForm.getHealthReport());

        if (healthReportForm.getHealthSportList() != null && healthReportForm.getHealthSportList().size() > 0) {
            for (HealthSport healthSport : healthReportForm.getHealthSportList()) {
                healthSport.setReportId(healthReportForm.getHealthReport().getId());
            }

            try {
                healthSportService.saveBatch(HealthSport.class, healthReportForm.getHealthSportList());
            } catch (Exception e) {
                e.printStackTrace();
                throw new MutilDeviceServiceException(ResponseEnum.ServerError);
            }
        }

    }
}
